package monitoreomedios

import org.springframework.dao.DataIntegrityViolationException

class CampanaController {

    static allowedMethods = [save: "POST"]

    def index() {
		if(session.usuario == null)
		{
			flash.message = "Para ingresar usted debe iniciar sesion."
			redirect(controller:"usuario", action:"login")
		}
		else
			redirect(action: "list", params: params)
    }

    def list() {
		
		if(session.usuario == null)
		{
			flash.message = "Para ingresar usted debe iniciar sesion."
			redirect(controller:"usuario", action:"login")
		}
		
		if(session.role == "user")
		{
	        params.max = Math.min(params.max ? params.int('max') : 10, 100)
			//println session.usuario
			
			def usuario = Usuario.findById(session.usuario)
			def lista = Campana.findAllByUsuario(usuario)
			def a = 0
			flash.tv = []
			flash.radio = []
			
			lista.each
			{
				def spots = SpotMonitoreado.findAllByCampana(it)
				flash.radio[a] = 0
				flash.tv[a] = 0
				spots.each
				{
					def radio = MencionMultimedia.findAllBySpotAndTipoDeMedio(it,"radio")
					flash.radio[a] += radio.size
					
					def tv = MencionMultimedia.findAllBySpotAndTipoDeMedio(it,"tv")
					flash.tv[a] += tv.size
				}
				a++
	        }
			
			
			def menciones = MencionMonitoreada.findAllByUsuario(usuario)
			def c = 0
			flash.mencion = []
			flash.cblog = []
			flash.cprensa = []
			flash.ctv = []
			flash.cradio = []
			flash.mid = []
			
			menciones.each
			{
				def prensa = MencionTexto.findAllByMencionAndTipoDeMedio(it, "prensa")
				def blog = MencionTexto.findAllByMencionAndTipoDeMedio(it, "blogs")
				def tv = MencionMultimedia.findAllByMencionAndTipoDeMedio(it, "tv")
				def radio = MencionMultimedia.findAllByMencionAndTipoDeMedio(it, "radio")
				
				println prensa
				println prensa.size
				
				flash.mencion[c] = it.frase
				flash.cblog[c] = blog.size
				flash.cprensa[c] = prensa.size
				flash.ctv[c] = tv.size
				flash.cradio[c] = radio.size
				flash.mid[c] = it.id
				c++
			}
			flash.mencionCount = menciones.size
	        [campanaInstanceList: lista, campanaInstanceTotal: lista.count]
		}
		else
		{
			//Si es el administrador
			def lista = Campana.findAll();
			println lista
			def menciones = MencionMonitoreada.findAll();
			def a = 0
			flash.tv = []
			flash.radio = []
			
			lista.each
			{
				def spots = SpotMonitoreado.findAllByCampana(it)
				flash.radio[a] = 0
				flash.tv[a] = 0
				spots.each
				{
					def radio = MencionMultimedia.findAllBySpotAndTipoDeMedio(it,"radio")
					flash.radio[a] += radio.size
					
					def tv = MencionMultimedia.findAllBySpotAndTipoDeMedio(it,"tv")
					flash.tv[a] += tv.size
				}
				a++
			}
			
			
			def c = 0
			flash.mencion = []
			flash.cblog = []
			flash.cprensa = []
			flash.ctv = []
			flash.cradio = []
			flash.mid = []
			flash.musuario = []
			
			menciones.each
			{
				def prensa = MencionTexto.findAllByMencionAndTipoDeMedio(it, "prensa")
				def blog = MencionTexto.findAllByMencionAndTipoDeMedio(it, "blogs")
				def tv = MencionMultimedia.findAllByMencionAndTipoDeMedio(it, "tv")
				def radio = MencionMultimedia.findAllByMencionAndTipoDeMedio(it, "radio")
				
				println prensa
				println prensa.size
				
				flash.mencion[c] = it.frase
				flash.cblog[c] = blog.size
				flash.cprensa[c] = prensa.size
				flash.ctv[c] = tv.size
				flash.cradio[c] = radio.size
				flash.mid[c] = it.id
				flash.musuario[c] = it.usuario
				c++
			}
			flash.mencionCount = menciones.size
			
			
			[campanaInstanceList: lista, campanaInstanceTotal: lista.count]
		}
    }

    def create() {
		
		if(session.usuario == null)
		{
			flash.message = "Para ingresar usted debe iniciar sesion."
			redirect(controller:"usuario", action:"login")
		}
		
        [campanaInstance: new Campana(params)]
    }

    def save() {
        def campanaInstance = new Campana(params)
        
		def user = Usuario.findById(session.usuario)
		user.addToCampanias(campanaInstance)
		if (!campanaInstance.save(flush: true)) {
            render(view: "create", model: [campanaInstance: campanaInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'campana.label', default: 'Campana'), campanaInstance.id])
        redirect(action: "list", id: campanaInstance.id)
    }
	
	
	def delete() {
		def campanaInstance = Campana.get(params.id)
		if (!campanaInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'campana.label', default: 'Campana'), params.id])
			redirect(action: "list")
			return
		}

		try {
			campanaInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'campana.label', default: 'Campana'), params.id])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'campana.label', default: 'Campana'), params.id])
			redirect(action: "list")
		}
	}
	
	
	
}
